package com.example.demo.controller;

import com.example.demo.entity.*;
import com.example.demo.service.GoodsService;
import com.example.demo.service.UserService;
import com.sun.org.apache.xpath.internal.operations.Or;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@CrossOrigin
@Controller
public class GoodsController {

    @Autowired
    GoodsService goodsService;

    @Autowired
    UserService userService;

    @CrossOrigin
    @PostMapping(value = "/api/listGoods")
    @ResponseBody
    public Result listGoods(@RequestBody Shop shop)
    {
        List<Goods> goods = goodsService.findAllGoods(shop);
        System.out.println(goods.toString());
        Result result = new Result(200);
        result.setData(goods);
        return result;
    }

    @GetMapping(value = "/api/listGoodsBySidAndMSg")
    @ResponseBody
    public Result listGoodsBySidAndMSgAndType(@RequestParam("id") int sid,
                                       @RequestParam("searchMsg") String searchMsg,
                                       @RequestParam("currentPage") int currentPage)
    {
        System.out.println(sid);
        List<Goods> goods = goodsService.findAllGoodsBySidAndMsg(sid,searchMsg,currentPage);
        int total = goodsService.findAllGoodsBySidAndMsgTotal(sid,searchMsg);

        for(Goods goods1 : goods){
            goods1.setType1(goodsService.getTypeById(Integer.parseInt(goods1.getType())));
        }
        Result result = new Result(200);
        result.setData(goods);
        result.setTotal(total);
        return result;
    }

    @CrossOrigin
    @PostMapping(value = "/api/listGoodsByType")
    @ResponseBody
    public Result listGoodsByType(@RequestBody Goods good)
    {
        List<Goods> goods = goodsService.findGoodsByType(good);
        Result result = new Result(200);
        result.setData(goods);
        return result;
    }


    @CrossOrigin
    @PostMapping(value = "/api/findGoodsById")
    @ResponseBody
    public Result findGoodsById(@RequestBody int id)
    {
        Goods goods = goodsService.findGoodsById(id);
        System.out.println(goods);
        Result result = new Result(200);
        result.setData(goods);
        return result;
    }

    /*
    num 更改的数量
    goodsid 商品编号
     */
    @CrossOrigin
    @GetMapping(value = "/api/updateGoodsNum")
    @ResponseBody
    public Result updateGoodsNum(@RequestBody Goods good)
    {
        goodsService.updateGoodsNum(good);
        Result result = new Result(200);

        return result;
    }

    @CrossOrigin
    @PostMapping(value = "/api/listGoodsEvaluate")
    @ResponseBody
    public Result findGoodsEvaluates(@RequestBody Evaluate evaluate)
    {
        List<Evaluate> evaluates = goodsService.findGoodsEvaluate(evaluate);
        for(int i = 0 ;i < evaluates.size();i++)
        {
            evaluates.get(i).setUsername(userService.findById(evaluates.get(i).getUserid()).getUsername());
        }
        Result result = new Result(200);
        result.setData(evaluates);
        return result;
    }

    @CrossOrigin
    @PostMapping(value = "/api/searchEvaluate")
    @ResponseBody
    public Result findGoodsEvaluate(@RequestBody Evaluate evaluate)
    {
        Evaluate evaluates = goodsService.searchEvaluate(evaluate);
        Result result = new Result(200);
        result.setData(evaluates);
        return result;
    }

    @CrossOrigin
    @PostMapping(value = "/api/insertGoodsEvaluate")
    @ResponseBody
    public Result insertGoodsEvaluate(@RequestBody Evaluate evaluate)
    {
        System.out.println(evaluate.toString());
        evaluate.setTime(new Date());
        goodsService.insertGoodsEvaluate(evaluate);
        Result result = new Result(200);
        return result;
    }

    @CrossOrigin
    @PostMapping(value = "/api/getTypeByName")
    @ResponseBody
    public Result getTypeByName(@RequestBody Type type)
    {
        Type type1 = goodsService.getTypeByName(type);
        Result result = new Result(200);
        result.setData(type1);
        return result;
    }

    @CrossOrigin
    @GetMapping(value = "/api/getAllTypes")
    @ResponseBody
    public Result getAllTypes()
    {
        List<Type> types = goodsService.getAllTypes();
        Result result = new Result(200);
        result.setData(types);
        return result;
    }

    @CrossOrigin
    @GetMapping(value = "/api/getAllTypesToG")
    @ResponseBody
    public OrderResult getAllTypesToG()
    {
        List<Type> types = goodsService.getAllTypes();
        List<Integer> values = new ArrayList<>();
        List<String> name = new ArrayList<>();
        for(int i = 0 ; i < types.size() ; i++)
        {
            OrderData orderData = new OrderData();
            values.add(types.get(i).getVolume());
            name.add(types.get(i).getType_name());
        }
        OrderResult result = new OrderResult();
        result.setData1(values);
        result.setData2(name);
        return result;
    }

    @CrossOrigin
    @PostMapping(value = "/api/insertType")
    @ResponseBody
    public Result insertType(@RequestBody Type type)
    {
        goodsService.insertType(type);
        Result result = new Result(200);
        return result;
    }

    @GetMapping(value = "/api/deleteGoodsById")
    @ResponseBody
    public void deleteGoodsById(@RequestParam int id)
    {
        goodsService.deleteGoodsById(id);
    }

    @PostMapping(value = "/api/editGoods")
    @ResponseBody
    public void editGoods(@RequestBody Goods goods)
    {
        goodsService.editGoods(goods);
    }

    @PostMapping(value = "/api/addGoods")
    @ResponseBody
    public void addGoods(@RequestBody Goods goods)
    {
        System.out.println(goods);
        goodsService.addGoods(goods);
    }





}
